//
//  UIStandardSelectView.m
//  vhStressTest1
//
//  Created by jia yu on 2021/8/31.
//

#import "UIStandardSelectViewController.h"
#import "UIStandardTableViewCell.h"
//#import "UIStandardPopoverBackgroundView.h"

@interface UIStandardSelectViewController()<UITableViewDelegate,UITableViewDataSource>{
    float scale;
    UITableView *myTableView;
    UIPopoverPresentationController *pop;
    float singleItemHeight;
}
@end

@implementation UIStandardSelectViewController
@synthesize handler;
@synthesize fromView;
@synthesize fromViewRect;
@synthesize availableItems;
@synthesize selectedString;
@synthesize maxWidth;
@synthesize maxHeight;
@synthesize itemFont;

-(void)setFromView:(UIView *)fromView{
    pop.sourceView = fromView;
}
-(void)setFromViewRect:(CGRect)fromViewRect{
    pop.sourceRect = fromViewRect;
}

-(id)initWithScale:(float)_scale{
    self = [super init];
    if (self){
        scale = _scale;
        self.view.backgroundColor = [UIColor whiteColor];
        self.modalPresentationStyle = UIModalPresentationPopover;
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        maxWidth = 300.f;
        maxHeight = 400.f;
        singleItemHeight = 68.f * scale;
        itemFont = [UIFont fontWithName:@"PingFangSC-Medium" size:34.f*scale];
//        pop = [[uIpopvi alloc] initWithPresentedViewController:self presentingViewController:self];
//        self.presentedPopoverController = pop;
        pop = self.popoverPresentationController;
//        [pop setPopoverBackgroundViewClass:[UIStandardPopoverBackgroundView class]];
        [pop setPermittedArrowDirections:UIPopoverArrowDirectionUp];
        pop.sourceView = self.view;
        pop.sourceRect = CGRectMake(100.f, 100.f, 30.f, 30.f);
    }
    return self;
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (selectedString){
        NSInteger selectedIndex = -1;
        for (NSInteger index =0;index<availableItems.count;index++){
            NSString *c = [availableItems objectAtIndex:index].str;
            if ([[c uppercaseString] isEqualToString:[selectedString uppercaseString]]){
                selectedIndex = index;
                break;
            }
        }
        if (selectedIndex >= 0){
            [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:selectedIndex inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
        }
    }
}

-(CGSize)preferredContentSize{
    float width = 100.f;
    for (NSItemObject *item in availableItems){
        CGSize size = [item.str sizeWithAttributes:@{NSFontAttributeName:itemFont}];
        if (width < size.width + 60.f){
            width = size.width + 60.f;
        }
        if (width > maxWidth){
            width = maxWidth;
            break;
        }
    }
    float height = singleItemHeight * availableItems.count;
    if (height > maxHeight){
        height = maxHeight;
    }
    return CGSizeMake(width, height);
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return availableItems.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = @"id";
    UIStandardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (nil == cell){
        cell = [[UIStandardTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.font = itemFont;
    }
    NSItemObject *item = [availableItems objectAtIndex:indexPath.row];
    cell.textLabel.text = item.str;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (handler){
        handler([availableItems objectAtIndex:indexPath.row]);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
    return;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return singleItemHeight;
}

@end
